package com.ruoyi.finance.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.WProductSalesInfoMapper;
import com.ruoyi.finance.domain.WProductSalesInfo;
import com.ruoyi.finance.service.IWProductSalesInfoService;

/**
 * 开票商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class WProductSalesInfoServiceImpl implements IWProductSalesInfoService 
{
    @Autowired
    private WProductSalesInfoMapper wProductSalesInfoMapper;

    /**
     * 查询开票商品
     * 
     * @param id 开票商品主键
     * @return 开票商品
     */
    @Override
    public WProductSalesInfo selectWProductSalesInfoById(Long id)
    {
        return wProductSalesInfoMapper.selectWProductSalesInfoById(id);
    }

    /**
     * 查询开票商品列表
     * 
     * @param wProductSalesInfo 开票商品
     * @return 开票商品
     */
    @Override
    public List<WProductSalesInfo> selectWProductSalesInfoList(WProductSalesInfo wProductSalesInfo)
    {
        return wProductSalesInfoMapper.selectWProductSalesInfoList(wProductSalesInfo);
    }

    /**
     * 新增开票商品
     *
     * @param wProductSalesInfo 开票商品
     * @return 结果
     */

    /**
     * @Author TianJiaKuan
     * @Description //TODO 税率的如果有特殊符号是返回异常
     * @Date 11:35 2025/3/5
     * @Param [wProductSalesInfo]
     * @return int
     **/

    @Override
    public int insertWProductSalesInfo(WProductSalesInfo wProductSalesInfo) {
        // 创建用户
        wProductSalesInfo.setCreateUser(SecurityUtils.getUsername());
        // 更新用户
        wProductSalesInfo.setUpdateUser(SecurityUtils.getUsername());
        //判断商品名是否为空
        if (wProductSalesInfo.getProductName() == null) {
            throw new RuntimeException("商品名称不能为空");
        }
        //判断商品规格是否为空
        if (wProductSalesInfo.getSpecificationModel() == null) {
            throw new RuntimeException("商品规格不能为空");
        }

        List<WProductSalesInfo> wProductSalesInfos = wProductSalesInfoMapper.selectWProductSalesInfoList(null);

        for (WProductSalesInfo info : wProductSalesInfos) {
            if (wProductSalesInfo.getProductName().equals(info.getProductName())) {
                if (wProductSalesInfo.getSpecificationModel().equals(info.getSpecificationModel())) {
                    throw new RuntimeException("相同名字的商品规格重复");
                }
            }
            if (wProductSalesInfo.getSpecificationModel().equals(info.getSpecificationModel())) {
                if (wProductSalesInfo.getProductName().equals(info.getProductName())) {
                    throw new RuntimeException("相同规格的商品名称重复");
                }
            }
        }

        //判断税率必须是整数
        if (wProductSalesInfo.getTaxRate().compareTo(new BigDecimal(0)) != 0 && wProductSalesInfo.getTaxRate().compareTo(new BigDecimal(0)) != 1 && wProductSalesInfo.getTaxRate().compareTo(new BigDecimal(0)) != -1){
            throw new RuntimeException("税率必须是整数");
        }

        //判断税率的范围0到100之前
        if (wProductSalesInfo.getTaxRate().compareTo(new BigDecimal(100)) > 0){
            throw new RuntimeException("税率范围在0到100之间");
        }
        if (wProductSalesInfo.getTaxRate().compareTo(new BigDecimal(0)) < 0){
            throw new RuntimeException("税率范围在0到100之间");
        }

//        wProductSalesInfo.setCreateTime(DateUtils.getNowDate());


        return wProductSalesInfoMapper.insertWProductSalesInfo(wProductSalesInfo);
    }

    /**
     * 修改开票商品
     * 
     * @param wProductSalesInfo 开票商品
     * @return 结果
     */
    @Override
    public int updateWProductSalesInfo(WProductSalesInfo wProductSalesInfo)
    {
        // 更新用户
        wProductSalesInfo.setUpdateUser(SecurityUtils.getUsername());
//        wProductSalesInfo.setUpdateTime(DateUtils.getNowDate());
        return wProductSalesInfoMapper.updateWProductSalesInfo(wProductSalesInfo);
    }

    /**
     * 批量删除开票商品
     * 
     * @param ids 需要删除的开票商品主键
     * @return 结果
     */
    @Override
    public int deleteWProductSalesInfoByIds(Long[] ids)
    {
        return wProductSalesInfoMapper.deleteWProductSalesInfoByIds(ids);
    }

    /**
     * 删除开票商品信息
     * 
     * @param id 开票商品主键
     * @return 结果
     */
    @Override
    public int deleteWProductSalesInfoById(Long id)
    {
        return wProductSalesInfoMapper.deleteWProductSalesInfoById(id);
    }
}
